package com.sunnada.edu.system.pub.util;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 
 * <p>
 * Description:解析XML文件转成map对象
 * </p>
 * 
 */
public class XmlUtils {

	/**
	 * 解析XML文件转成map对象
	 * 
	 * @param xml
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> xmlToMap(String path) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			File f = new File(path);
			SAXReader reader = new SAXReader();
			Document doc = reader.read(f);
			Element root = doc.getRootElement();
			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				Element element = it.next();
				map.put(element.getName(), element.getTextTrim());
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return map;
	}

	

	public static void main(String[] a) {
		xmlToMap("C://test.xml");
	}
}
